﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Data;

namespace EasyCheque.CommonPages
{
    public partial class ChequeDrop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bool needReturn = true;
            if (Session["chequeIDToDrop"] == null)
            {
                needReturn = true;
            }
            else
            {
                needReturn = false;
            }
            Table_Return.Visible = needReturn;
        }

        protected void Button_ResetPassword_Click(object sender, EventArgs e)
        {
            // 在这里作废支票。
            Label_CommitMessage.Visible = true;
            if (Session["chequeIDToDrop"] != null)
            {
                if (Session["uid"] == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
                string CmdStr = @"SELECT [PASSWORD] FROM PERSON_INFO WHERE ID=" + Session["uid"].ToString();
                DataSet DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count != 1)
                {
                    Label_CommitMessage.Text = "内部数据库错误";
                    return;
                }
                if (!Security.MD5.Exam(TextBox_PASSWORD.Text, System.Text.Encoding.ASCII, DS.Tables[0].Rows[0][0].ToString()))
                {
                    Label_CommitMessage.Text = "登录密码错误";
                    return;
                }
                CmdStr = @"UPDATE CHEQUE_INFO SET [STATE]=2 WHERE [CHEQUE_ID]=" + Session["chequeIDToDrop"].ToString();
                if (!Logic.Logic.UpdateData(CmdStr))
                {
                    Label_CommitMessage.Text = "内部数据库错误";
                    return;
                }
                CmdStr = @"SELECT [INSURANCE],[MONEY],[ACCOUNT_ID] FROM CHEQUE_INFO WHERE [CHEQUE_ID] = " + Session["chequeIDToDrop"].ToString();
                DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count != 1)
                {
                    Label_CommitMessage.Text = "内部数据库错误";
                    return;
                }
                if (DS.Tables[0].Rows[0][0].ToString().ToUpper() == "TRUE")
                {
                    string ChqAOC = DS.Tables[0].Rows[0][1].ToString();
                    string ChqAcc = DS.Tables[0].Rows[0][2].ToString();
                    CmdStr = @"UPDATE ACCOUNT_INFO SET [USEABLE]=[USEABLE]-" + ChqAOC + ",[MONEY]=[MONEY]+" + ChqAOC + " WHERE ACCOUNT_ID='" + ChqAcc + "'";
                    if (!Logic.Logic.UpdateData(CmdStr))
                    {
                        Label_CommitMessage.Text = "内部数据库错误";
                        return;
                    }
                }
                Label_CommitMessage.Visible = false;
                Response.Redirect("~/Cheque/ManageCheque.aspx");
            }
        }
    }
}
